package com.woniuxy.kaer.service.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.kaer.commons.util.Data;
import com.woniuxy.kaer.commons.util.Result;
import com.woniuxy.kaer.redis.util.RedisKey;
import com.woniuxy.kaer.service.entity.ServiceEvaluate;
import com.woniuxy.kaer.service.entity.ServiceOrder;
import com.woniuxy.kaer.service.mapper.ServiceEvaluateMapper;
import com.woniuxy.kaer.service.mapper.ServiceOrderMapper;
import com.woniuxy.kaer.service.service.ServiceOrderService;
import com.woniuxy.kaer.tenants.dto.ShopInfoDto;
import com.woniuxy.kaer.tenants.feign.TenantsFeignClient;
import com.woniuxy.kaer.tenants.from.ChangeShopScoreFrom;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Component
public class DemoJobHandler {
    @Resource
    private ServiceOrderService serviceOrderService;
    @Resource
    private ServiceOrderMapper serviceOrderMapper;
    @Resource
    private TenantsFeignClient tenantsFeignClient;
    @Resource
    private ServiceEvaluateMapper serviceEvaluateMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @XxlJob("orderExpiredHandler")
    public void DetermineExpired() throws Exception {
        String param = XxlJobHelper.getJobParam();
        ServiceOrder serviceOrder = serviceOrderMapper.selectById(param);
        if(serviceOrder.getServiceOrderState()== Data.SERVICE_ORDER_STATE_ACCEPT){
            serviceOrder.setServiceOrderState(Data.SERVICE_ORDER_STATE_OVERDUE);
            serviceOrderMapper.updateById(serviceOrder);
        }
        System.out.println("任务执行成功");
    }

    @XxlJob("confirmsOrderHandler")
    public void confirmsOrder() throws Exception {
        String param = XxlJobHelper.getJobParam();
//        ServiceOrder serviceOrder = serviceOrderMapper.selectById(param);
        QueryWrapper<ServiceOrder> serviceOrderQueryWrapper = new QueryWrapper<>();
        serviceOrderQueryWrapper.eq("service_order_id",param);
        ServiceOrder serviceOrder = serviceOrderMapper.selectOne(serviceOrderQueryWrapper);
        if(serviceOrder.getServiceOrderState()== Data.SERVICE_ORDER_STATE_PROCESSING){
            serviceOrder.setServiceOrderState(Data.SERVICE_ORDER_STATE_COMPLETE);
            serviceOrderMapper.updateById(serviceOrder);
        }
        System.out.println("任务执行成功");
    }
    //商家统计每天晚上自己的三个月的预约订单数量，订单完结数量，总销售额
    @XxlJob("shopServiceCount")
    public void shopServiceCount() throws Exception {
        Result<List<ShopInfoDto>> allShopInfo = tenantsFeignClient.getAllShopInfo();
        for (ShopInfoDto shopInfoDto : allShopInfo.getData()) {

            Integer shopId = shopInfoDto.getShopId();
            if(stringRedisTemplate.hasKey(RedisKey.SHOP_ORDER_INFO(shopId))){
                stringRedisTemplate.delete(RedisKey.SHOP_ORDER_INFO(shopId));
            }
            //查询三个月的预约订单数量
            Integer makeOrderNumByShopId90Days = serviceOrderMapper.getMakeOrderNumByShopId90Days(shopId);
            //查询三个月的完结订单数量
            Integer takeOrderNumByShopId90Days = serviceOrderMapper.getTakeOrderNumByShopId90Days(shopId);
            //查询三个月的销售总额
            BigDecimal orderSaleAmountByShopId90Days = serviceOrderMapper.getOrderSaleAmountByShopId90Days(shopId);

            //stringRedisTemplate.opsForZSet().add(RedisKey.USER_CAR_LIST(userId), String.valueOf(bookId), new Date().getTime());
            HashMap<Object, Object> HashMap = new HashMap<>();
            HashMap.put("makeOrderNumByShopId90Days", String.valueOf(makeOrderNumByShopId90Days));
            HashMap.put("takeOrderNumByShopId90Days", String.valueOf(takeOrderNumByShopId90Days));
            HashMap.put("orderSaleAmountByShopId90Days", String.valueOf(orderSaleAmountByShopId90Days));
            stringRedisTemplate.opsForHash().putAll(RedisKey.SHOP_ORDER_INFO(shopId), HashMap);
            //
            //stringRedisTemplate.opsForHash().get(RedisKey.SHOP_ORDER_INFO(shopId), "makeOrderNumByShopId90Days").toString();
        }
        System.out.println("任务执行成功");

    }
    @XxlJob("ChangeShopServiceScore")
    public void shopServiceScore() throws Exception {
        Result<List<ShopInfoDto>> allShopInfo = tenantsFeignClient.getAllShopInfo();
        System.out.println();
        List<ShopInfoDto> shopInfoDtoList = allShopInfo.getData();
        System.out.println("查询的商铺数量"+shopInfoDtoList.size());
        for (ShopInfoDto shopInfoDto : shopInfoDtoList) {
            System.out.println("店铺id"+shopInfoDto.getShopId());
            //计算每个商店的评分数
            QueryWrapper<ServiceEvaluate> serviceEvaluateQueryWrapper = new QueryWrapper<>();
            serviceEvaluateQueryWrapper.eq("shop_id",shopInfoDto.getShopId());
            Long aLong = serviceEvaluateMapper.selectCount(serviceEvaluateQueryWrapper);
            QueryWrapper<ServiceEvaluate> serviceEvaluateQueryWrapper1 = new QueryWrapper<>();
            serviceEvaluateQueryWrapper1.eq("shop_id",shopInfoDto.getShopId());
            //计算所有订单评分之和
            List<ServiceEvaluate> serviceEvaluateList = serviceEvaluateMapper.selectList(serviceEvaluateQueryWrapper1);
            Double score = 0.0;
            for (ServiceEvaluate serviceEvaluate : serviceEvaluateList) {
                score += serviceEvaluate.getServiceOrderEvaluateScore().doubleValue();
            }
            Double newShopScore = score/Double.valueOf(aLong);
            QueryWrapper<ServiceOrder> serviceOrderQueryWrapper = new QueryWrapper<>();
            serviceOrderQueryWrapper.eq("shop_id",shopInfoDto.getShopId());
            serviceOrderQueryWrapper.eq("service_order_state",Data.SERVICE_ORDER_STATE_COMPLETE);
            serviceOrderQueryWrapper.or();
            serviceOrderQueryWrapper.eq("service_order_state",Data.SERVICE_ORDER_STATE_RATED);
            Long aLong1 = serviceOrderMapper.selectCount(serviceOrderQueryWrapper);
            ChangeShopScoreFrom changeShopScoreFrom = new ChangeShopScoreFrom();
            changeShopScoreFrom.setShopId(shopInfoDto.getShopId());
            changeShopScoreFrom.setShopScore(newShopScore);
            changeShopScoreFrom.setServiceCount(aLong1.intValue());
            System.out.println("店铺id"+shopInfoDto.getShopId()+"店铺评分"+newShopScore+"店铺服务数量"+aLong1);
            tenantsFeignClient.shopServiceCount(changeShopScoreFrom);

        }

    }

}
